package com.css.fxfzypg.modules.suggestinfo.manage.emergencydisposalManage.controller;


import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.suggestinfo.manage.emergencydisposalManage.repository.entity.EmergencyDisposalManageEntity;
import com.css.fxfzypg.modules.suggestinfo.manage.emergencydisposalManage.service.EmergencyDisposalManageService;
import com.css.fxfzypg.modules.suggestinfo.manage.vo.ParamVo;
import com.css.fxfzypg.modules.system.service.UserCacheService;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.web.PlatformSessionContext;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/emergencyDisposalManage")
@Slf4j
public class EmergencyDisposalManageController {

    @Autowired
    EmergencyDisposalManageService emergencyDisposalManageService;
    @Autowired
    UserCacheService userCacheService;

    /**
     * 保存
     * @param emergencyDisposalManageEntity
     * @return
     */
    @PostMapping("/save")
    public RestResponse save(@RequestBody EmergencyDisposalManageEntity emergencyDisposalManageEntity) {
        RestResponse response = null;
        try{
            Map<String, Object> map = emergencyDisposalManageService.save(emergencyDisposalManageEntity);
            boolean res = ((Boolean) map.get("res")).booleanValue();
            if(res){
                response = RestResponse.succeed("保存成功!");
                response.setMessage("保存成功!");
            }else {
                response = RestResponse.succeed("保存失败!");
                response.setMessage(map.get("mess").toString());
            }
        }catch (Exception e){
            String errormessage = "保存失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 数据列表查询
     * @param request
     * @param paramVo
     * @return
     */
    @GetMapping("/getDataList")
    public RestResponse getDataList(HttpServletRequest request, ParamVo paramVo) {
        RestResponse response = null;
        try{
            String currUid = PlatformSessionContext.getUserID();
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            switch (userCacheService.getUserRoleType(currUid)) {
                case DATA_ROLES_PROVINCE:
                    SUser user = userCacheService.getUserFromCache(currUid);
                    String provinceId = user.getProvinceId();
                    if (StringUtils.isBlank(provinceId)) {
                        return RestResponse.fail("找不到当前省级用户的所在省信息");
                    }
                    paramVo.setProvinceId(provinceId);
                    break;
                case DATA_ROLES_OTHER:
                    return RestResponse.fail("未知角色");
                default:
                    break;
            }
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> map = emergencyDisposalManageService.getDataList(paramVo,curPage,pageSize);
            response = RestResponse.succeed(map);
            response.setMessage("查询区县致死性差异性指标列表数据成功!");
        }catch (Exception e){
            log.error("查询区县致死性差异性指标列表数据失败!",e);
            response=RestResponse.fail("查询区县致死性差异性指标列表数据失败!");
        }
        return response;
    }

    /**
     * 根据id查询详情页
     * @param id
     * @return
     */
    @GetMapping("/getDataById")
    public RestResponse getDataById(String id) {
        RestResponse response = null;
        try{
            EmergencyDisposalManageEntity annualRiskManageEntity = emergencyDisposalManageService.getDataById(id);
            response = RestResponse.succeed(annualRiskManageEntity);
            response.setMessage("获取详情页数据成功");
        }catch (Exception e){
            log.error("获取详情页数据失败!",e);
            response=RestResponse.fail("获取详情页数据失败!");
        }
        return response;
    }

    /**
     * 修改
     * @param emergencyDisposalManageEntity
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody EmergencyDisposalManageEntity emergencyDisposalManageEntity) {
        RestResponse response = null;
        try{
            Map<String, Object> map = emergencyDisposalManageService.update(emergencyDisposalManageEntity);
            boolean res = ((Boolean) map.get("res")).booleanValue();
            if(res){
                response = RestResponse.succeed("修改成功!");
                response.setMessage("修改成功!");
            }else {
                response = RestResponse.succeed("修改失败!");
                response.setMessage(map.get("mess").toString());
            }
        }catch (Exception e){
            String errormessage = "修改失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 删除
     * @param idList
     * @return
     */
    @PostMapping("/delete")
    public RestResponse delete(@RequestBody List<String> idList) {
        RestResponse response = null;
        try{
            boolean res = emergencyDisposalManageService.delete(idList);
            if(res){
                response = RestResponse.succeed("删除成功!");
                response.setMessage("删除成功!");
            }else {
                response = RestResponse.fail("删除失败!");
            }
        }catch (Exception e){
            String errormessage = "删除失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }


}
